<?php

namespace App\Common\Util;

use App\Common\Model\Base\Administrators;
use App\Common\Model\Model;

/**
 * Class Auth
 */
class AdminAuth
{
    private const AUTH_KEY = "_ADMIN_AUTH_";

    /**
     * @template User
     * @param string|User $user
     *
     * @return User|Administrators
     */
    public static function getInfo(string $user = Administrators::class): mixed
    {
        return  TP::session()->get(self::AUTH_KEY);
    }

    /**
     * 验证
     *
     * @return bool|false
     */
    public static function verify(): bool
    {
        return TP::session()->has(self::AUTH_KEY);
    }

    /**
     * 判断超管
     *
     * @return bool
     */
    public static function isSuper(): bool
    {
        $user = self::getInfo();

        return $user?->id == 1 && $user instanceof Administrators;
    }

    /**
     * 判断用户类型
     *
     * @param string|class-string $userModel
     * @return bool
     * @author chenlong<vip_chenlong@163.com>
     * @date 2023/1/20 11:48
     */
    public static function is(string $userModel): bool
    {
        return self::getInfo() instanceof $userModel;
    }

    /**
     * 保存认证信息
     *
     * @param Model $model
     * @return void
     */
    public static function save(Model $model): void
    {
        TP::session()->set(self::AUTH_KEY, $model);
    }

    /**
     * 清除认证信息
     *
     * @return void
     */
    public static function clear(): void
    {
        TP::session()->delete(self::AUTH_KEY);
    }
}
